package geppetto.phraseHMM.phraseExtraction.extractedphrase.data;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


public class LexicalWeightData extends ExtractedPhrasePairData{

	double lexicalWeighting;
	
	public LexicalWeightData(double lexicalWeighting) {
		super();
		this.lexicalWeighting = lexicalWeighting;
	}	

	public double getLexicalWeighting() {
		return lexicalWeighting;
	}

	public void setLexicalWeighting(double lexicalWeighting) {
		this.lexicalWeighting = lexicalWeighting;
	}

	@Override
	public void merge(ArrayList<HashMap<String, ExtractedPhrasePairData>> generalData, ArrayList<ExtractedPhrasePairData> data) {
		if(!data.contains(this)){
			data.add(this);
		}
		
		double[] values = new double[data.size()];
		int[] counts = new int[data.size()];
		int n = 0;
		
		for(ExtractedPhrasePairData d : data){
			if(d instanceof LexicalWeightData){
				double lex = ((LexicalWeightData) d).getLexicalWeighting();
				int index = -1;
				for(int i = 0; i < values.length; i++){
					if(values[i] == lex){
						index = i;
					}
				}
				if(index < 0){
					values[n] = lex;
					counts[n] = 1;
					n++;
				}
				else{
					counts[index]++;
				}
			}
			else{
				System.err.println("trying to merge an instance of LexicalWeightData to with another type of class");
				System.exit(-1);
			}
		}
		
		int max=0;
		int maxIndex=0;
		for(int i=0;i<n;i++){
			if(counts[i] > max){
				max = counts[i];
				maxIndex = i;
			}
		}
		setLexicalWeighting(values[maxIndex]);
	}

	@Override
	public String toString() {
		return String.valueOf(lexicalWeighting);
	}
	
	public static LexicalWeightData parseString(String s) {
		return new LexicalWeightData(Double.parseDouble(s));
	}

}
